package com.hotel.servlet;

/**
 * @author Eumenides.
 * @create 2021-05-11 15:18
 */

import com.hotel.bean.Account;
import com.hotel.dao.AccountDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 链接数据库
        SqlSession sqlSession = DBOperation.getSqlSession();
        //创建AccountDao
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);

        String action = request.getParameter("action");
        System.out.println(action);
        Account account = new Account();
        switch (action) {
            //判断用户名重复
            case "checkUser":

                account.setUsername(request.getParameter("userName"));
                Account accountUserCheck = accountDao.findByName(account);
                System.out.println(accountUserCheck);
                if (accountUserCheck == null) {
                    response.getWriter().append("true");
                } else {
                    response.getWriter().append("false");
                }
                break;
            //判断手机号重复
            case "checkPhone":
                account.setPhone(request.getParameter("phoneNumber"));
                Account accountPhoneCheck = accountDao.findByPhone(account);
                System.out.println(accountPhoneCheck);
                if (accountPhoneCheck == null) {
                    response.getWriter().append("true");
                } else {
                    response.getWriter().append("false");
                }
                break;
            //判断邮箱重复
            case "checkEmail":
                account.setEmail(request.getParameter("Email"));
                System.out.println("Email: " + account.getEmail());
                Account accountEmailCheck = accountDao.findByEmail(account);
                System.out.println("accountEmailCheck: " + accountEmailCheck);
                if(accountEmailCheck == null) {
                    response.getWriter().append("true");
                } else {
                    response.getWriter().append("false");
                }
                break;
            //成功注册
            case "reg":
                //使用前台数据，创建Account实体
                account.setUsername(request.getParameter("username"));
                account.setPhone(request.getParameter("phoneNo"));
                account.setEmail(request.getParameter("Email"));
                account.setPassword(request.getParameter("pwd"));
                account.setCreate_day(new Date());
                System.out.println(account);
//              同步判断一次
                if (accountDao.findByAccount(account).size() == 0) {
                    accountDao.saveUser(account);
                } else {
                    request.getRequestDispatcher("jsp/register.jsp").forward(request, response);
                }
                //转到登录界面
                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
                break;
        }
        //关闭数据库链接
        sqlSession.close();
    }
}
